package com.lipiao.smarthome;

import android.annotation.SuppressLint;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.transition.Explode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lipiao.smarthome.Utils.InforUtil;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;
    private Button btGo;
    private CardView cv;
    private TextView status;
    private FloatingActionButton fab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

             InforUtil.getInforUtil("sunpangbo5");

        setListener();
    }
    private void initView() {
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        status=findViewById(R.id.status);
        btGo = findViewById(R.id.bt_go);
        cv = findViewById(R.id.cv);
        fab = findViewById(R.id.fab);
    }

    private void setListener() {
        btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Explode explode = new Explode();
//                explode.setDuration(300);
//                getWindow().setExitTransition(explode);
//                getWindow().setEnterTransition(explode);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //登陆逻辑
                            OkHttpClient client =new OkHttpClient();
                            RequestBody requestBody=new FormBody.Builder()
                                    .add("user_name",etUsername.getText().toString())
                                    .add("password",etPassword.getText().toString())
                                    .build();
                            Request request=new Request.Builder()
                                    .url("http://47.106.164.70:8889/login")
                                    .post(requestBody)
                                    .build();

                            Response response =client.newCall(request).execute();
                            String responseData=response.body().string();
                            Log.d("接口调式", "返回的数据"+responseData);

                            Intent i2 = new Intent(LoginActivity.this,MainActivity.class);
                            i2.putExtra("etUsername",etUsername.getText().toString());
                            i2.putExtra("etPassword",etPassword.getText().toString());
                            startActivity(i2);

//                            if(responseData.equals("登录成功")){
//                                status.setText("登录成功！");
//                                Intent i2 = new Intent(LoginActivity.this,MainActivity.class);
//                                i2.putExtra("etUsername",etUsername.getText().toString());
//                                i2.putExtra("etPassword",etPassword.getText().toString());
//                                startActivity(i2);
//                            }
//                            else {
//                                status.setText("账号名或者密码错误哦！");
//                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWindow().setExitTransition(null);
                getWindow().setEnterTransition(null);
                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(LoginActivity.this, fab, fab.getTransitionName());
                startActivity(new Intent(LoginActivity.this, RegistActivity.class), options.toBundle());
            }
        });
    }
    @SuppressLint("RestrictedApi")
    @Override
    protected void onRestart() {
        super.onRestart();
        fab.setVisibility(View.GONE);
    }

    @SuppressLint("RestrictedApi")
    @Override
    protected void onResume() {
        super.onResume();
        fab.setVisibility(View.VISIBLE);
    }
}
